//
//  M_UserInfoVC.swift
//  letuofu
//
//  Created by WH-JS on 2020/1/13.
//  Copyright © 2020 WH-JS. All rights reserved.
//

import UIKit

class M_UserInfoVC: BaseViewControllerWithHeader {
    let nameLab = UILabel()
    let groupLab = UILabel()
    let phoneLab = UILabel()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupUI()
        getUserInfo()
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */
    
    func getUserInfo() {
        let params :[String:Any] = ["identityToken":LocalStorage.getString(forKey: USER_IDENTITYTOKEN) ?? ""]
        API.request(url: .M_GetUserInfo, params: params) { (result) in
            if result.success {
               if let dl = result.data?.getDictionary("data") {
                self.nameLab.text = dl.getString("name")
                self.groupLab.text = dl.getString("departmentName")
                self.phoneLab.text = dl.getString("mobile")
               }
            }else{
                let popup = PopupSureBox(title: result.errMsg ?? "登录失败，请重新登录", sureText: "确定")
                popup.show(vc: UIViewController.topMostController() ?? self)
                popup.onSureButtonClick = {
                    //确定执行
                }
            }
        }
    }
}

// MARK: UI
extension M_UserInfoVC {
    func setupUI() {
        safeView.backgroundColor = .xF2F2F2
        headView.backgroundColor = .white
        headTitle.text = "个人信息"
        
//        drawRightButton(imageName: "xiaoxi_4")
//        .addTarget(self, action: #selector(_xiaoxiButtonClick(button:)), for: .touchUpInside)
        let newsButton = drawRightButton(imageName: "xiaoxi_4")
        newsButton.addTarget(self, action: #selector(_xiaoxiButtonClick(button:)), for: .touchUpInside)
        UnreadPointForNewsButton(button: newsButton, pointRadius: 3.5,
                                 pointOffset: .init(x: -9.5, y: 10),  color: .xE0551B)

        var bgview = safeView.drawView { (make) in
            make.top.equalTo(headView.snp.bottom).offset(10)
            make.left.right.equalToSuperview()
            make.height.equalTo(51.5)
        }
        bgview.backgroundColor = .white
        bgview.drawLabel(text: "姓名", fontSize: 12, fontWeight: .regular, textColor: .x333333) { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(10)
        }
        bgview.drawLabel(label: nameLab,text: "", fontSize: 12, fontWeight: .regular, textColor: .x333333) { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-10)
        }
        //画横线
        var line = bgview.drawView { (make) in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        line.backgroundColor = .xF2F2F2
        
        bgview = safeView.drawView { (make) in
            make.top.equalTo(bgview.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(51.5)
        }
        bgview.backgroundColor = .white
        bgview.drawLabel(text: "所属组织", fontSize: 12, fontWeight: .regular, textColor: .x333333) { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(10)
        }
        bgview.drawLabel(label: groupLab,text: "", fontSize: 12, fontWeight: .regular, textColor: .x333333) { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-10)
        }
        //画横线
        line = bgview.drawView { (make) in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        line.backgroundColor = .xF2F2F2
        
        bgview = safeView.drawView { (make) in
            make.top.equalTo(bgview.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(75)
        }
        bgview.backgroundColor = .white
        bgview.drawLabel(text: "手机号码", fontSize: 12, fontWeight: .regular, textColor: .x333333) { (make) in
            make.centerY.equalToSuperview().offset(-10)
            make.left.equalToSuperview().offset(10)
        }
        bgview.drawLabel(label: phoneLab,text: "", fontSize: 12, fontWeight: .regular, textColor: .x333333) { (make) in
            make.centerY.equalToSuperview().offset(-10)
            make.right.equalToSuperview().offset(-10)
        }
        bgview.drawLabel(text: "*如需修改手机号，请您提交书面申请到人事备案", fontSize: 12, fontWeight: .regular, textColor: .x999999) { (make) in
            make.centerY.equalToSuperview().offset(15)
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
        }.numberOfLines = 2
    }
}

// MARK: 事件
extension M_UserInfoVC {
    @objc func _xiaoxiButtonClick(button: Button) {
        let vc = M_NewsListVC()
        pushViewController(vc)
    }
}
